package travelDream.beans;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.FlowEvent;

import traveldream.ejb.DTO.ExcursionDTO;
import traveldream.ejb.DTO.HotelDTO;
import traveldream.ejb.DTO.PackageDTO;
import traveldream.ejb.DTO.TransportationDTO;
import traveldream.ejb.interfaces.PackageCustomizeBeanLocal;
import traveldream.ejb.interfaces.PackageInsertionBeanLocal;;


@ManagedBean(name = "PackageCM")
@SessionScoped
public class PackageCustomizeManaged {
	
	private List<HotelDTO> hDTO;
	private int selectedH;
	private List<TransportationDTO> gDTO;
	private int selectedG;
	private List<TransportationDTO> rDTO;
	private int selectedR;
	private List<ExcursionDTO> eDTO;
	private int selectedE;
	private PackageDTO pDTO;
	
	private boolean type;
	private String city;
	private int nOfPeople;
	
	@EJB
	PackageCustomizeBeanLocal PackageCustomizeBeanLocal;
	
	@EJB
	PackageInsertionBeanLocal PackageInsertionBeanLocal;
	
	public String onload(String city, PackageDTO pDTO, int nOfPeople){
		if(nOfPeople<1){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "You have to buy at least 1 package!!"));
			return "";}
		this.nOfPeople = nOfPeople;
		this.pDTO = pDTO;
		this.city = city;
		selectedH = pDTO.getIdHotel();
		selectedE = pDTO.getIdExcursion();
		selectedG = pDTO.getIdGoing();
		selectedR = pDTO.getIdReturn();
		System.out.println("ON LOAD:"+selectedR);
		gDTO = PackageCustomizeBeanLocal.going(city,nOfPeople);
		return "packageCustomizeHotel.xhtml?faces-redirect=true";
	}
	
	public long dayBetweenTwoDate(Date uno, Date due) {
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(uno);
		c2.setTime(due);

		long giorni = (c2.getTime().getTime() - c1.getTime()
				.getTime())	/ (24 * 3600 * 1000);

		return giorni;
	}
	
	private void updatePrice(){
		HotelDTO h = new HotelDTO();
		TransportationDTO g = new TransportationDTO();
		TransportationDTO r = new TransportationDTO();;
		ExcursionDTO e = new ExcursionDTO();
		
		for(ExcursionDTO sel: eDTO){
			if(sel.getId() == selectedE){
				e = sel;
			}
		}
		for(TransportationDTO sel: gDTO){
			if(sel.getId() == selectedG){
				g = sel;
			}
		}
		for(TransportationDTO sel: rDTO){
			if(sel.getId() == selectedR){
				r = sel;
			}
		}
		for(HotelDTO sel: hDTO){
			if(sel.getId() == selectedH){
				h = sel;
			}
		}

		pDTO.setPrice(h.getPrice()*((int) dayBetweenTwoDate(g.getArrivalDate(),r.getDepartureDate()) + g.getPrice()+r.getPrice()+e.getPrice()));
	}

	public String onFlowProcess(FlowEvent event) {  
		if(event.getOldStep().equals("going")){
			rDTO = PackageCustomizeBeanLocal.ret(selectedG, nOfPeople);
		}else if(event.getOldStep().equals("return")){
			hDTO = PackageCustomizeBeanLocal.Hotel(selectedG,selectedR, nOfPeople);
		}else if(event.getOldStep().equals("hotel")){
			if(hDTO==null){System.out.println(event.getSource().toString()); return event.getOldStep();}
			eDTO = PackageCustomizeBeanLocal.excursion(selectedG,selectedR,nOfPeople);
		}else if(event.getOldStep().equals("excursion")){
			save();
		}
        return event.getNewStep();  
    }  
	
	
	public void save(){
		pDTO.setIdExcursion(selectedE);
		pDTO.setIdGoing(selectedG);
		pDTO.setIdReturn(selectedR);
		pDTO.setIdHotel(selectedH);
		updatePrice();
	}
	
	public void transportationFlow(){
		
	}

	public List<HotelDTO> gethDTO() {
		return hDTO;
	}

	public void sethDTO(List<HotelDTO> hDTO) {
		this.hDTO = hDTO;
	}

	public int getSelectedH() {
		return selectedH;
	}

	public void setSelectedH(int selectedH) {
		this.selectedH = selectedH;
	}

	public int getSelectedG() {
		return selectedG;
	}

	public void setSelectedG(int selectedG) {
		this.selectedG = selectedG;
	}

	public int getSelectedR() {
		return selectedR;
	}

	public void setSelectedR(int selectedR) {
		this.selectedR = selectedR;
	}

	public List<ExcursionDTO> geteDTO() {
		return eDTO;
	}

	public void seteDTO(List<ExcursionDTO> eDTO) {
		this.eDTO = eDTO;
	}

	public int getSelectedE() {
		return selectedE;
	}

	public void setSelectedE(int selectedE) {
		this.selectedE = selectedE;
	}

	public boolean isType() {
		return type;
	}

	public void setType(boolean type) {
		this.type = type;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public int getnOfPeople() {
		return nOfPeople;
	}

	public void setnOfPeople(int nOfPeople) {
		this.nOfPeople = nOfPeople;
	}

	public PackageDTO getpDTO() {
		return pDTO;
	}

	public void setpDTO(PackageDTO pDTO) {
		this.pDTO = pDTO;
	}

	public List<TransportationDTO> getgDTO() {
		return gDTO;
	}

	public void setgDTO(List<TransportationDTO> gDTO) {
		this.gDTO = gDTO;
	}

	public List<TransportationDTO> getrDTO() {
		return rDTO;
	}

	public void setrDTO(List<TransportationDTO> rDTO) {
		this.rDTO = rDTO;
	}
	
	
	
}
